Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


The method starts out by verifying that the given column number is valid. If it is not, an exception is thrown. Some other types of initialization are also performed. Remember that all ResultSet objects are provided with a Hashtable of SimpleTextColumn objects describing each column:

protected int verify(
     int column)
    throws SQLException
{
     clearWarnings();
    lastNull = false;

   SimpleTextColumn col = (SimpleTextColumn) inMemoryColumns.get(
                                    new Integer(column));

    if (col == null) {
         throw new SQLException("Invalid column number: " + column);
  }
    return col.colNo;
}

Next, if the row data is stored in an in-memory Hashtable (as with the DatabaseMetaData catalog methods), the data is retrieved from the Hashtable. Otherwise, the driver gets the data from the data file. In both instances, the data is retrieved as a CommonValue object, and the getString method is used to format the data into the requested data type. Null values are handled specially; the JDBC API has a wasNull method that will return true if the last column that was retrieved was null:

public boolean wasNull()
    throws SQLException
{
    return lastNull;
}

The SimpleText driver also supports InputStreams. In our case, the SimpleTextInputStream class is just a simple wrapper around a CommonValue object. Thus, if an application requests the data for a column as an InputStream, the SimpleText driver will get the data as a CommonValue object (as it always does) and create an InputStream that fetches the data from the CommonValue.

The getMetaData method returns a ResultSetMetaData object, which is our last class to cover.

ResultSetMetaData

The ResultSetMetaData class provides methods that describe each one of the columns in a result set. This includes the column count, column attributes, and the column name. ResultSetMetaData will typically be the smallest class in a JDBC driver, and is usually very straightforward to implement. For the SimpleText driver, all of the necessary information is retrieved from the Hashtable of column information that is required for all result sets. Thus, to retrieve the column name:

public   String getColumnLabel(
       int column)
      throws SQLException
{
     // Use the column name
      return getColumnName(column);
}

protected SimpleTextColumn getColumn(
    int col)
   throws SQLException
{
    SimpleTextColumn column = (SimpleTextColumn)
                        inMemoryColumns.get(new Integer(col));

    if (column == null) {
        throw new SQLException("Invalid column number: " + col);
    }

    return column;
}

Summary

We have covered a lot of material in this chapter, including the JDBC DriverManager and the services that it provides, implementing Java interfaces, creating native JDBC drivers, tracing, data coercion, escape sequence processing, and each one of the major JDBC interfaces. This information, in conjunction with the SimpleText driver, should help you to create your own JDBC driver without too much difficulty.


Previous Table of Contents Next